//
//  GridLoginInputView.swift
//  grid-ios-app
//
//  Created by yanlu on 2024/3/17.
//

import UIKit
import SnapKit

enum GridLoginInptutType {
    case name
    case password
}

internal protocol GridLoginInputViewDelegate: NSObjectProtocol {
    
    func gridLoginInputView(view: GridLoginInputView, nameText: String?, type: GridLoginInptutType);
}

class GridLoginInputView: UIView {
    
    
    
    internal weak var delegate: GridLoginInputViewDelegate?

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupUI()
    }
    
    func setupUI() {
        backgroundColor = .clear
        addSubview(nameTextField)
        addSubview(passwordTextField)

        nameTextField.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(10)
            make.left.equalToSuperview().offset(20)
            make.right.equalToSuperview().offset(-20)
            make.height.equalTo(60)
        }
        
        passwordTextField.snp.makeConstraints { make in
            make.top.equalTo(nameTextField.snp.bottom).offset(20)
            make.left.equalToSuperview().offset(20)
            make.right.equalToSuperview().offset(-20)
            make.height.equalTo(60)
        }
    }
    
    lazy var nameTextField: UITextField = {
        let textField = UITextField(frame: .zero)
        textField.font = Theme.Font.size(20, bold: false)
        textField.textColor = UIColor(hex6: 0x333333)
        textField.placeholder = "请输入用户名"
        textField.leftView = GridLeftView(frame: CGRect(x: 0, y: 0, width: 40, height: 30), name: "me", szie: CGSize(width: 18, height: 18))
        textField.leftViewMode = .always
        textField.clearButtonMode = .whileEditing
        textField.layer.masksToBounds = true
        textField.layer.cornerRadius = 30
        textField.backgroundColor = UIColor(hex6: 0xF3F3F3)
        textField.addTarget(self, action: #selector(handleEditingChanged(textField:)), for: .editingChanged)
        return textField
    }()
    
    lazy var passwordTextField: UITextField = {
        let textField = UITextField(frame: .zero)
        textField.font = Theme.Font.size(20, bold: false)
        textField.textColor = UIColor(hex6: 0x333333)
        textField.placeholder = "请输入密码"
        textField.leftView = GridLeftView(frame: CGRect(x: 0, y: 0, width: 40, height: 30), name: "me_1", szie: CGSize(width: 18, height: 18))
        textField.leftViewMode = .always
        textField.clearButtonMode = .whileEditing
        textField.layer.masksToBounds = true
        textField.layer.cornerRadius = 30
        textField.isSecureTextEntry = true
        textField.backgroundColor = UIColor(hex6: 0xF3F3F3)
        textField.addTarget(self, action: #selector(handleEditingChanged(textField:)), for: .editingChanged)
        return textField
    }()
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

    @objc
    func handleEditingChanged(textField: UITextField) {
        var textType: GridLoginInptutType = .password
        if textField == self.nameTextField {
            textType = .name
        }
        delegate?.gridLoginInputView(view: self, nameText: textField.text, type: textType)
    }
    
}


class GridLeftView: UIView {
    private var leftImageName: String!
    private var leftSize: CGSize!

    public convenience init(frame: CGRect, name: String, szie: CGSize) {
        self.init(frame: frame)
        backgroundColor = .clear
        self.frame = frame
        leftImageName = name
        leftSize = szie
        imageView.image = UIImage(named: leftImageName ?? "")
        imageView.bounds = CGRect(x: 0, y: 0, width: leftSize.width, height: leftSize.height)
        imageView.center = self.center
        addSubview(imageView)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    lazy var imageView: UIImageView = {
        let imageView = UIImageView()
        imageView.backgroundColor = .clear
        return imageView
    }()
}
